package com.drven.mq.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Resource
    private AmqpTemplate rabbitAmqpTemplate;

    //exchange交换器名称
    @Value("${mq.config.exchange}")
    private String exchange;
    //routingkey路由键
    @Value("${mq.config.queue.info.routing.key}")
    private String routingkey;


    @GetMapping("/send")
    public void send(String message) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(1000);
                    rabbitAmqpTemplate.convertAndSend(exchange, routingkey, "第" + i++ + "次向你发送消息问：你好吗？");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
